package com.donleo.ssm.controller;

import com.donleo.ssm.utils.LayTransferResult;
import com.donleo.ssm.model.TeamModel;
import com.donleo.ssm.model.UserModel;
import com.donleo.ssm.service.ITeamService;
import com.donleo.ssm.utils.ResponseLayResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author liangd
 * date 2020-11-25 16:41
 * code 小组前端控制层
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private ITeamService teamService;

    /**
     * 增加
     * @param teamModel
     * @return
     */
    @PostMapping("/add")
    public ResponseLayResult add(@RequestBody TeamModel teamModel){
        Integer count = teamService.add(teamModel);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseLayResult delete(Integer id){
        Integer count = teamService.delete(id);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/deletes")
    public ResponseLayResult deletes(Integer[] ids){
       List<Integer> idsList= Arrays.asList(ids);
       Integer count = teamService.deletes(idsList);
       return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 修改
     * @param teamModel
     * @return
     */
    @PostMapping("/update")
    public ResponseLayResult update(@RequestBody TeamModel teamModel){
        Integer count = teamService.update(teamModel);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseLayResult findById(Integer id){
        TeamModel teamModel = teamService.findById(id);
        return ResponseLayResult.createBySuccess(1,teamModel);
    }

    /**
     * 模糊查询
     * @param teamModel
     * @return
     */
    @GetMapping("/findAll")
    public ResponseLayResult findAll(TeamModel teamModel){
        List<TeamModel> list = teamService.findAll(teamModel);
        Integer count = teamService.getCount(teamModel);
        return ResponseLayResult.createBySuccess(count,list);
    }

    /**
     * 查询所有未分组学生
     * @return
     */
    @GetMapping("/findAllStudent")
    public ResponseLayResult findAllStudent(){
        List<UserModel> list = teamService.findAllStudent();
        Integer count = teamService.getStudentCount();
        return ResponseLayResult.createBySuccess(count,list);
    }

    /**
     * 分配组员
     * @param list 集合对象
     * @param id 小组ID
     * @return
     */
    @PostMapping("/assignStudent")
    public ResponseLayResult assignStudent(@RequestBody List<LayTransferResult> list,
                                           @RequestParam Integer id){
        Integer count = teamService.assignStudent(list,id);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 查询小组成员
     * @param id 小组Id
     * @return
     */
    @GetMapping("/findStudentByTeam")
    public ResponseLayResult findStudentByTeam(Integer id){
        List<UserModel> list= teamService.findStudentByTeam(id);
        Integer count = teamService.getStudentTeamCount(id);
        return ResponseLayResult.createBySuccess(count,list);
    }

    /**
     * 移除组员
     * @param list
     * @param id
     * @return
     */
    @PostMapping("/deleteStudent")
    public ResponseLayResult deleteStudent(@RequestBody List<LayTransferResult> list,
                                           @RequestParam Integer id){
        Integer count = teamService.deleteStudent(list,id);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 分配组长
     * @param teamModel
     * @return
     */
    @PostMapping("/updateTeamLeader")
    public ResponseLayResult updateTeamLeader(@RequestBody TeamModel teamModel){
        Integer count = teamService.updateTeamLeader(teamModel);
        return ResponseLayResult.createBySuccess(count,count);
    }
}
